package com.burst.reptile.modular.api.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.api.form.AddRedBody;
import com.burst.reptile.modular.api.vo.ArticleDto;
import com.burst.reptile.modular.system.entity.SysUser;
import com.burst.reptile.modular.system.service.ISysUserService;
import com.burst.reptile.modular.zp.entity.ZpResources;
import com.burst.reptile.modular.zp.service.IZpResourcesService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 招商数据封装
 * @author wujin
 *
 * @date: 2021年6月26日 下午2:11:29
 */
@RestController
@RequestMapping("/api/zs")
public class ZsApiController extends BaseController{
	
	@Autowired
	private IZpResourcesService zpResourcesService;
	
	@Autowired
	private ISysUserService userService;

	/**
	 * 推荐文章列表
	 * @return
	 */
	@GetMapping("/list")
	public LayuiResult<Object> list(){
		Page<ZpResources> page = startPage();
		Page<ZpResources> pageList = zpResourcesService.apiPageList(page, "1", 1);
		List<ZpResources> records = pageList.getRecords();
		List<ArticleDto> list = new ArrayList<>();
		if(ToolUtil.isNotEmpty(records)) {
			for (ZpResources zpResources : records) {
				ArticleDto dto = new ArticleDto();
				BeanUtil.copyProperties(zpResources, dto);
				Long userId = zpResources.getUserId();
				SysUser byId = userService.getById(userId);
				if(ToolUtil.isNotEmpty(byId)) {
					dto.setAuthor(byId.getNickName());
					dto.setAvatar(byId.getAvatar());
				}
				if(StrUtil.isNotBlank(zpResources.getPosters())) {
					dto.setVideoPoster(JSONObject.parseArray(zpResources.getPosters(), String.class));
				}
				if(StrUtil.isNotBlank(zpResources.getCoverImage())) {
					dto.setType("1");
				}else {
					dto.setType("0");
				}
				if(StrUtil.isNotBlank(zpResources.getVideoUrl())) {
					dto.setType("3");
					dto.setVideos(JSONObject.parseArray(zpResources.getVideoUrl(), String.class));
				}
				if(StrUtil.isNotBlank(zpResources.getPictureSet())) {
					List<String> parseArray = JSONObject.parseArray(zpResources.getPictureSet(), String.class);
					dto.setImgs(parseArray);
					if(parseArray.size() > 2) {
						dto.setType("2");
					}
				}
				list.add(dto);
			}
		}
//		return LayuiResult.success(pageList.hasNext(), pageList.getTotal(), list);
		return LayuiResult.success(false, 0L, null);
	}
	
	/**
	 * 添加阅读数
	 * @return
	 */
	@PostMapping("/add/red")
	public Result<Object> addRed(@RequestBody AddRedBody body){
		int incrRead = zpResourcesService.incrRead(body.getId());
		return Result.success(incrRead);
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("/details")
	public Result<Object> details(@RequestParam Long id){
		ZpResources resources = zpResourcesService.getById(id);
		ArticleDto dto = null;
		if(ToolUtil.isNotEmpty(resources)) {
			dto = new ArticleDto();
			BeanUtil.copyProperties(resources, dto);
			Long userId = resources.getUserId();
			SysUser byId = userService.getById(userId);
			if(ToolUtil.isNotEmpty(byId)) {
				dto.setAuthor(byId.getNickName());
				dto.setAvatar(byId.getAvatar());
			}
			if(StrUtil.isNotBlank(resources.getPosters())) {
				dto.setVideoPoster(JSONObject.parseArray(resources.getPosters(), String.class));
			}
			if(StrUtil.isNotBlank(resources.getCoverImage())) {
				dto.setType("1");
			}else {
				dto.setType("0");
			}
			if(StrUtil.isNotBlank(resources.getVideoUrl())) {
				dto.setType("3");
				dto.setVideos(JSONObject.parseArray(resources.getVideoUrl(), String.class));
			}
			if(StrUtil.isNotBlank(resources.getPictureSet())) {
				List<String> parseArray = JSONObject.parseArray(resources.getPictureSet(), String.class);
				dto.setImgs(parseArray);
				if(parseArray.size() > 2) {
					dto.setType("2");
				}
			}
			zpResourcesService.incrRead(id);
		}
		return Result.success(dto);
	}
	 
}
